##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##  Bradley Zeis
##  flamingoengine@gmail.com

import pygame
from ... import constants as flconstants

type_map = {
    pygame.constants.QUIT: flconstants.QUIT,
    pygame.constants.VIDEOEXPOSE: flconstants.NOEVENT,
    pygame.constants.VIDEORESIZE: flconstants.NOEVENT,
    pygame.constants.KEYDOWN: flconstants.KEYDOWN,
    pygame.constants.KEYUP: flconstants.KEYUP,
    pygame.constants.MOUSEMOTION: flconstants.MOUSEMOTION,
    pygame.constants.MOUSEBUTTONDOWN: flconstants.MOUSEBUTTONDOWN,
    pygame.constants.MOUSEBUTTONUP: flconstants.MOUSEBUTTONUP,
    pygame.constants.JOYAXISMOTION: flconstants.NOEVENT,
    pygame.constants.JOYBALLMOTION: flconstants.NOEVENT,
    pygame.constants.JOYHATMOTION: flconstants.NOEVENT,
    pygame.constants.JOYBUTTONUP: flconstants.NOEVENT,
    pygame.constants.JOYBUTTONDOWN: flconstants.NOEVENT
}

key_map = {
    pygame.constants.KMOD_ALT: flconstants.K_ALT,
    pygame.constants.KMOD_CAPS: flconstants.K_CAPS,
    pygame.constants.KMOD_CTRL: flconstants.K_CTRL,
    pygame.constants.KMOD_LALT: flconstants.K_LALT,
    pygame.constants.KMOD_LCTRL: flconstants.K_LCTRL,
    pygame.constants.KMOD_LMETA: flconstants.K_LMETA,
    pygame.constants.KMOD_LSHIFT: flconstants.K_LSHIFT,
    pygame.constants.KMOD_META: flconstants.K_META,
    pygame.constants.KMOD_NUM: flconstants.K_NUM,
    pygame.constants.KMOD_RALT: flconstants.K_RALT,
    pygame.constants.KMOD_RCTRL: flconstants.K_RCTRL,
    pygame.constants.KMOD_RMETA: flconstants.K_RMETA,
    pygame.constants.KMOD_RSHIFT: flconstants.K_RSHIFT,
    pygame.constants.KMOD_SHIFT: flconstants.K_SHIFT,
    pygame.constants.K_CAPSLOCK: flconstants.K_CAPS,
    pygame.constants.K_LALT: flconstants.K_LALT,
    pygame.constants.K_LCTRL: flconstants.K_LCTRL,
    pygame.constants.K_LMETA: flconstants.K_LMETA,
    pygame.constants.K_LSHIFT: flconstants.K_LSHIFT,
    pygame.constants.K_RALT: flconstants.K_RALT,
    pygame.constants.K_RCTRL: flconstants.K_RCTRL,
    pygame.constants.K_RMETA: flconstants.K_RMETA,
    pygame.constants.K_RSHIFT: flconstants.K_RSHIFT,
    pygame.constants.K_0: flconstants.K_0,
    pygame.constants.K_1: flconstants.K_1,
    pygame.constants.K_2: flconstants.K_2,
    pygame.constants.K_3: flconstants.K_3,
    pygame.constants.K_4: flconstants.K_4,
    pygame.constants.K_5: flconstants.K_5,
    pygame.constants.K_6: flconstants.K_6,
    pygame.constants.K_7: flconstants.K_7,
    pygame.constants.K_8: flconstants.K_8,
    pygame.constants.K_9: flconstants.K_9,
    pygame.constants.K_AMPERSAND: flconstants.K_AMPERSAND,
    pygame.constants.K_ASTERISK: flconstants.K_ASTERISK,
    pygame.constants.K_AT: flconstants.K_AT,
    pygame.constants.K_BACKQUOTE: flconstants.K_BACKQUOTE,
    pygame.constants.K_BACKSLASH: flconstants.K_BACKSLASH,
    pygame.constants.K_BACKSPACE: flconstants.K_BACKSPACE,
    pygame.constants.K_BREAK: flconstants.K_BREAK,
    pygame.constants.K_CAPSLOCK: flconstants.K_CAPSLOCK,
    pygame.constants.K_CARET: flconstants.K_CARET,
    pygame.constants.K_CLEAR: flconstants.K_CLEAR,
    pygame.constants.K_COLON: flconstants.K_COLON,
    pygame.constants.K_COMMA: flconstants.K_COMMA,
    pygame.constants.K_DELETE: flconstants.K_DELETE,
    pygame.constants.K_DOLLAR: flconstants.K_DOLLAR,
    pygame.constants.K_DOWN: flconstants.K_DOWN,
    pygame.constants.K_END: flconstants.K_END,
    pygame.constants.K_EQUALS: flconstants.K_EQUALS,
    pygame.constants.K_ESCAPE: flconstants.K_ESCAPE,
    pygame.constants.K_EURO: flconstants.K_EURO,
    pygame.constants.K_EXCLAIM: flconstants.K_EXCLAIM,
    pygame.constants.K_F1: flconstants.K_F1,
    pygame.constants.K_F10: flconstants.K_F10,
    pygame.constants.K_F11: flconstants.K_F11,
    pygame.constants.K_F12: flconstants.K_F12,
    pygame.constants.K_F13: flconstants.K_F13,
    pygame.constants.K_F14: flconstants.K_F14,
    pygame.constants.K_F15: flconstants.K_F15,
    pygame.constants.K_F2: flconstants.K_F2,
    pygame.constants.K_F3: flconstants.K_F3,
    pygame.constants.K_F4: flconstants.K_F4,
    pygame.constants.K_F5: flconstants.K_F5,
    pygame.constants.K_F6: flconstants.K_F6,
    pygame.constants.K_F7: flconstants.K_F7,
    pygame.constants.K_F8: flconstants.K_F8,
    pygame.constants.K_F9: flconstants.K_F9,
    pygame.constants.K_GREATER: flconstants.K_GREATER,
    pygame.constants.K_HOME: flconstants.K_HOME,
    pygame.constants.K_INSERT: flconstants.K_INSERT,
    pygame.constants.K_KP0: flconstants.K_KP0,
    pygame.constants.K_KP1: flconstants.K_KP1,
    pygame.constants.K_KP2: flconstants.K_KP2,
    pygame.constants.K_KP3: flconstants.K_KP3,
    pygame.constants.K_KP4: flconstants.K_KP4,
    pygame.constants.K_KP5: flconstants.K_KP5,
    pygame.constants.K_KP6: flconstants.K_KP6,
    pygame.constants.K_KP7: flconstants.K_KP7,
    pygame.constants.K_KP8: flconstants.K_KP8,
    pygame.constants.K_KP9: flconstants.K_KP9,
    pygame.constants.K_KP_DIVIDE: flconstants.K_KP_DIVIDE,
    pygame.constants.K_KP_ENTER: flconstants.K_KP_ENTER,
    pygame.constants.K_KP_EQUALS: flconstants.K_KP_EQUALS,
    pygame.constants.K_KP_MINUS: flconstants.K_KP_MINUS,
    pygame.constants.K_KP_MULTIPLY: flconstants.K_KP_MULTIPLY,
    pygame.constants.K_KP_PERIOD: flconstants.K_KP_PERIOD,
    pygame.constants.K_KP_PLUS: flconstants.K_KP_PLUS,
    pygame.constants.K_LAST: flconstants.K_LAST,
    pygame.constants.K_LEFT: flconstants.K_LEFT,
    pygame.constants.K_LEFTBRACKET: flconstants.K_LEFTBRACKET,
    pygame.constants.K_LEFTPAREN: flconstants.K_LEFTPAREN,
    pygame.constants.K_LESS: flconstants.K_LESS,
    pygame.constants.K_LSUPER: flconstants.K_LSUPER,
    pygame.constants.K_MENU: flconstants.K_MENU,
    pygame.constants.K_MINUS: flconstants.K_MINUS,
    pygame.constants.K_MODE: flconstants.K_MODE,
    pygame.constants.K_NUMLOCK: flconstants.K_NUMLOCK,
    pygame.constants.K_PAGEDOWN: flconstants.K_PAGEDOWN,
    pygame.constants.K_PAGEUP: flconstants.K_PAGEUP,
    pygame.constants.K_PAUSE: flconstants.K_PAUSE,
    pygame.constants.K_PERIOD: flconstants.K_PERIOD,
    pygame.constants.K_PLUS: flconstants.K_PLUS,
    pygame.constants.K_POWER: flconstants.K_POWER,
    pygame.constants.K_PRINT: flconstants.K_PRINT,
    pygame.constants.K_QUESTION: flconstants.K_QUESTION,
    pygame.constants.K_QUOTE: flconstants.K_QUOTE,
    pygame.constants.K_QUOTEDBL: flconstants.K_QUOTEDBL,
    pygame.constants.K_RETURN: flconstants.K_RETURN,
    pygame.constants.K_RIGHT: flconstants.K_RIGHT,
    pygame.constants.K_RIGHTBRACKET: flconstants.K_RIGHTBRACKET,
    pygame.constants.K_RIGHTPAREN: flconstants.K_RIGHTPAREN,
    pygame.constants.K_RSUPER: flconstants.K_RSUPER,
    pygame.constants.K_SCROLLOCK: flconstants.K_SCROLLOCK,
    pygame.constants.K_SEMICOLON: flconstants.K_SEMICOLON,
    pygame.constants.K_SLASH: flconstants.K_SLASH,
    pygame.constants.K_SPACE: flconstants.K_SPACE,
    pygame.constants.K_SYSREQ: flconstants.K_SYSREQ,
    pygame.constants.K_TAB: flconstants.K_TAB,
    pygame.constants.K_UNDERSCORE: flconstants.K_UNDERSCORE,
    pygame.constants.K_UNKNOWN: flconstants.K_UNKNOWN,
    pygame.constants.K_UP: flconstants.K_UP,
    pygame.constants.K_a: flconstants.K_a,
    pygame.constants.K_b: flconstants.K_b,
    pygame.constants.K_c: flconstants.K_c,
    pygame.constants.K_d: flconstants.K_d,
    pygame.constants.K_e: flconstants.K_e,
    pygame.constants.K_f: flconstants.K_f,
    pygame.constants.K_g: flconstants.K_g,
    pygame.constants.K_h: flconstants.K_h,
    pygame.constants.K_i: flconstants.K_i,
    pygame.constants.K_j: flconstants.K_j,
    pygame.constants.K_k: flconstants.K_k,
    pygame.constants.K_l: flconstants.K_l,
    pygame.constants.K_m: flconstants.K_m,
    pygame.constants.K_n: flconstants.K_n,
    pygame.constants.K_o: flconstants.K_o,
    pygame.constants.K_p: flconstants.K_p,
    pygame.constants.K_q: flconstants.K_q,
    pygame.constants.K_r: flconstants.K_r,
    pygame.constants.K_s: flconstants.K_s,
    pygame.constants.K_t: flconstants.K_t,
    pygame.constants.K_u: flconstants.K_u,
    pygame.constants.K_v: flconstants.K_v,
    pygame.constants.K_w: flconstants.K_w,
    pygame.constants.K_x: flconstants.K_x,
    pygame.constants.K_y: flconstants.K_y,
    pygame.constants.K_z: flconstants.K_z
}

mb_map = {
    1: flconstants.MB_1,
    2: flconstants.MB_2,
    3: flconstants.MB_3,
    4: flconstants.MS_UP,
    5: flconstants.MS_DOWN,
}

def init():
    pass

def poll():
    events = pygame.event.get()
    l = []
    
    for event in events:
        try: e = [-1, type_map[event.type]]
        except KeyError: continue
        
        if event.type == pygame.constants.QUIT:
            e = [-2, -2]
            
        elif event.type == pygame.constants.MOUSEMOTION:
            e[0] = flconstants.M_MOTION
        
        elif event.type == pygame.constants.KEYDOWN or event.type == pygame.constants.KEYUP:
            try: e[0] = key_map[event.key]
            except KeyError: continue
            
        elif event.type == pygame.constants.MOUSEBUTTONDOWN or event.type == pygame.constants.MOUSEBUTTONUP:
            e[0] = mb_map[event.button]
            
            
        l.append(e)
        
    return l
            
